//
// * Copyright (c) 2002-2009 "Neo Technology,"
// *     Network Engine for Objects in Lund AB [http://neotechnology.com]
// *
// * This file is part of Neo4j.
// * 
// * Neo4j is free software: you can redistribute it and/or modify
// * it under the terms of the GNU Affero General Public License as
// * published by the Free Software Foundation, either version 3 of the
// * License, or (at your option) any later version.
// * 
// * This program is distributed in the hope that it will be useful,
// * but WITHOUT ANY WARRANTY; without even the implied warranty of
// * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// * GNU Affero General Public License for more details.
// * 
// * You should have received a copy of the GNU Affero General Public License
// * along with this program. If not, see <http://www.gnu.org/licenses/>.
// 
namespace org.neo4j.kernel.impl.transaction.xaframework
{


	internal class DirectMappedLogBuffer : LogBuffer
	{
		private const int BUFFER_SIZE = 1024 * 1024 * 2;

		private readonly FileChannel fileChannel;

		private ByteBuffer byteBuffer = null;
		private long bufferStartPosition;

//JAVA TO VB & C# CONVERTER WARNING: Method 'throws' clauses are not available in .NET:
//ORIGINAL LINE: DirectMappedLogBuffer(FileChannel fileChannel) throws IOException
		internal DirectMappedLogBuffer(FileChannel fileChannel)
		{
			this.fileChannel = fileChannel;
			bufferStartPosition = fileChannel.position();
			byteBuffer = ByteBuffer.allocateDirect(BUFFER_SIZE);
			getNewMappedBuffer();
		}

//JAVA TO VB & C# CONVERTER WARNING: Method 'throws' clauses are not available in .NET:
//ORIGINAL LINE: private void getNewMappedBuffer() throws IOException
		private void getNewMappedBuffer()
		{
			byteBuffer.flip();
			bufferStartPosition += fileChannel.write(byteBuffer, bufferStartPosition);
			byteBuffer.Clear();
		}

//JAVA TO VB & C# CONVERTER WARNING: Method 'throws' clauses are not available in .NET:
//ORIGINAL LINE: public LogBuffer put(byte b) throws IOException
		public virtual LogBuffer put(sbyte b)
		{
			if (byteBuffer == null || (BUFFER_SIZE - byteBuffer.position()) < 1)
			{
				getNewMappedBuffer();
			}
			byteBuffer.put(b);
			return this;
		}

//JAVA TO VB & C# CONVERTER WARNING: Method 'throws' clauses are not available in .NET:
//ORIGINAL LINE: public LogBuffer putInt(int i) throws IOException
		public virtual LogBuffer putInt(int i)
		{
			if (byteBuffer == null || (BUFFER_SIZE - byteBuffer.position()) < 4)
			{
				getNewMappedBuffer();
			}
			byteBuffer.putInt(i);
			return this;
		}

//JAVA TO VB & C# CONVERTER WARNING: Method 'throws' clauses are not available in .NET:
//ORIGINAL LINE: public LogBuffer putLong(long l) throws IOException
		public virtual LogBuffer putLong(long l)
		{
			if (byteBuffer == null || (BUFFER_SIZE - byteBuffer.position()) < 8)
			{
				getNewMappedBuffer();
			}
			byteBuffer.putLong(l);
			return this;
		}

//JAVA TO VB & C# CONVERTER WARNING: Method 'throws' clauses are not available in .NET:
//ORIGINAL LINE: public LogBuffer put(byte[] bytes) throws IOException
		public virtual LogBuffer put(sbyte[] bytes)
		{
			if (byteBuffer == null || (BUFFER_SIZE - byteBuffer.position()) < bytes.Length)
			{
				getNewMappedBuffer();
			}
			byteBuffer.put(bytes);
			return this;
		}

//JAVA TO VB & C# CONVERTER WARNING: Method 'throws' clauses are not available in .NET:
//ORIGINAL LINE: public LogBuffer put(char[] chars) throws IOException
		public virtual LogBuffer put(char[] chars)
		{
			if (byteBuffer == null || (BUFFER_SIZE - byteBuffer.position()) < (chars.Length * 2))
			{
				getNewMappedBuffer();
			}
			int oldPos = byteBuffer.position();
			byteBuffer.asCharBuffer().put(chars);
			byteBuffer.position(oldPos + chars.Length * 2);
			return this;
		}

//JAVA TO VB & C# CONVERTER WARNING: Method 'throws' clauses are not available in .NET:
//ORIGINAL LINE: public void force() throws IOException
		public virtual void force()
		{
			getNewMappedBuffer();
			fileChannel.force(false);
		}

		public virtual long getFileChannelPosition()
		{
			if (byteBuffer != null)
			{
				return bufferStartPosition + byteBuffer.position();
			}
			return bufferStartPosition;
		}

		public virtual FileChannel getFileChannel()
		{
			return fileChannel;
		}
	}
}